<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Add_contact2 extends Page
{
       public $lblCity = null;
       public $lblAddress = null;
       public $cmbCountry = null;
       public $Label5 = null;
       public $Label4 = null;
       public $edCity = null;
       public $edAreaFax = null;
       public $Label3 = null;
       public $compid = null;
       public $btnAddJob = null;
       public $edJob = null;
       public $listJob = null;
       public $JTDivJob = null;
       public $JSJobTitle3 = null;
       public $JSJobTitle2 = null;
       public $JSJobTitle1 = null;
       public $JTGroupBox3 = null;
       public $btnDelDept = null;
       public $btnShowJob = null;
       public $cmbJob = null;
       public $lblJob = null;
       public $lblarea = null;
       public $edArea = null;
       public $Label2 = null;
       public $JSProvinceMunicipality = null;
       public $JSRegionProvince = null;
       public $lble = null;
       public $lblp = null;
       public $lbld = null;
       public $lbll = null;
       public $lblst = null;
       public $lblm = null;
       public $lblf = null;
       public $Label1 = null;
       public $hfError = null;
       public $JSValidate = null;
       public $btnContactCancel = null;
       public $btnSubmit = null;
       public $hiddenVend = null;
       public $JTBdate = null;
       public $JTSiteTheme1 = null;
       public $edEmail = null;
       public $edFax = null;
       public $cmbMunicipality = null;
       public $cmbProvince = null;
       public $cmbRegion = null;
       public $edBldg = null;
       public $edAddress = null;
       public $edMobile = null;
       public $edPhone = null;
       public $edContactDesignation = null;
       public $edContactLname = null;
       public $edContactMname = null;
       public $edContactFname = null;
       public $Label8 = null;
       public $Label30 = null;
       public $Label29 = null;
       public $Label28 = null;
       public $Label27 = null;
       public $Label26 = null;
       public $Label25 = null;
       public $Label24 = null;
       public $Label23 = null;
       public $Label22 = null;
       public $Label21 = null;
       public $Label20 = null;
       public $Label19 = null;
       public $Label12 = null;
       public $Label10 = null;
       function edCityJSBlur($sender, $params)
       {

       ?>
       validateRequire('edCity','lblCity');

       <?php

       }


       function btnDelDeptJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var JobIndex = document.getElementById('listJob').selectedIndex;
       var JobValue = document.getElementById('listJob')[JobIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxJob3(JobValue,comp);
       <?php

       }

       function btnAddJobJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addjob = document.getElementById('edJob').value;
       var comp = document.getElementById('compid').value;
       AjaxJob2(addjob,comp);
       document.getElementById('edJob').value = "";
       <?php

       }

       function btnShowJobJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivJob').Show();
       document.getElementById('edJob').value = "";
       <?php

       }

       function edAreaJSBlur($sender, $params)
       {

       ?>
       var edArea = document.getElementById('edArea').value;

       if(edArea.length>0)
       {
         regex_pattern = "^[0-9]{2,4}$";
         var reg = new RegExp(regex_pattern);
         var match = document.getElementById('edArea').value.match(reg);
         if(match==null)
         {
          document.getElementById('lblarea').innerHTML = "Incorrect format - e.g. 02&nbsp;&darr;";
         }
         else
         {
          document.getElementById('lblarea').innerHTML = "&nbsp;";
         }
         }
       else
       {
         document.getElementById('lblarea').innerHTML = "&darr; This field is required &darr;";
       }
       <?php

       }
       function edEmailJSBlur($sender, $params)
       {

       ?>
       //validateRequire('edEmail','lble');

       var edEmail = document.getElementById('edEmail').value;

       if(edEmail.length>0)
       {
       regex_pattern = "[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$";
       var regm = new RegExp(regex_pattern);
       var match = document.getElementById('edEmail').value.match(regm);
       if(match==null)
       {
        document.getElementById('lble').innerHTML = "&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;";
       }
       }
       <?php

       }

       function edAddressJSBlur($sender, $params)
       {

       ?>
       validateRequire('edAddress','lblAddress');
       <?php

       }

       function edPhoneJSBlur($sender, $params)
       {

       ?>
       validateRequire('edPhone','lblp');
       var edPhone = document.getElementById('edPhone').value;

       if(edPhone.length>0)
       {
       regex_pattern = "^[0-9]{3}[-]?[0-9]{4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edPhone').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblp').innerHTML = "&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblp').innerHTML = "";
       }
       }
       <?php

       }

       function edContactDesignationJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edContactDesignation','lbld');
       <?php

       }

       function edContactLnameJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edContactLname','lbll');
       <?php

       }

       function edContactMnameJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edContactMname','lblm');
       <?php

       }

       function edContactFnameJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edContactFname','lblf');
       <?php

       }

       function Add_contact2JSLoad($sender, $params)
       {
       $this->clr_reg();
       ?>
       document.getElementById('edBldg').value = window.opener.document.getElementById('edBldg').value;
       document.getElementById('edAddress').value = window.opener.document.getElementById('edAddress').value;
       document.getElementById('edCity').value = window.opener.document.getElementById('edCity').value;
       document.getElementById('edArea').value = window.opener.document.getElementById('edArea').value;
       document.getElementById('edPhone').value = window.opener.document.getElementById('edCompPhone').value;
       <?php

       }


       function cmbProvinceJSChange($sender, $params)
       {
       //echo $this->cmbProvince->ajaxCall("VendorProvinceChange");
       ?>
       //Add your javascript code here
       provinceMunicipality();
       return false;
       <?php

       }

       function cmbRegionJSChange($sender, $params)
       {
       //echo $this->cmbRegion->ajaxCall("VendorRegionChange");
       ?>
       //Add your javascript code here
       regionProvince();
       return false;
       <?php

       }



       function clr_reg()
       {
       $this->edContactFname->Text = '';
       $this->edContactMname->Text = '';
       $this->edContactLname->Text = '';
       $this->edContactDesignation->Text = '';
       $this->edArea->Text = '';
       $this->edPhone->Text = '';
       $this->edMobile->Text = '';
       $this->edAddress->Text = '';
       $this->edBldg->Text = '';
       $this->edFax->Text = '';
       $this->edEmail->Text = '';
       $this->JTBdate->Text= '';

       /*
       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $this->cmbRegion->setItemIndex('--Select--');
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }

        $this->cmbProvince->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
        */
       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }

       function Add_contact2Create($sender, $params)
       {
       $this->clr_reg();
       $this->hfError->setValue('0');
       $this->lblf->Caption = '';
       $this->lblm->Caption = '';
       $this->lbll->Caption = '';
       $this->lblp->Caption = '';
       $this->lble->Caption = '';
       $this->lblst->Caption = '';
       $this->lbld->Caption = '';
       $this->lblarea->Caption = '';

       $this->lblCity->Caption = '';
       $this->lblAddress->Caption = '';

       $this->edCity->Text = '';
       $this->edAreaFax->Text = '';

       $this->cmbCountry->Clear();
       $this->cmbCountry->ItemIndex = "Philippines";
       $query_country = "SELECT country FROM countries";
       $result_country = mysql_query($query_country) or die("SELECT COUNTRIES ".mysql_error());
       while($row_country = mysql_fetch_array($result_country))
       {
        $country = $row_country['country'];
        $this->cmbCountry->AddItem($country, '', $country);
       }

       $this->compid->Value = $_SESSION['company_id'];
       $vendor = $_GET['vend_id'];
       $this->hiddenVend->setValue($vendor);

       $this->cmbJob->Clear();
        $this->cmbJob->AddItem('--Select--', null, null);
        $this->cmbJob->setItemIndex('--Select--');
        $result_job = mysql_query("select * from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='job titles' order by value asc");

        while($row_job = mysql_fetch_array($result_job))
        {
            $this->cmbJob->AddItem($row_job['value'], null, $row_job['value']);
        }

       //list Job
        $this->listJob->Clear();
        $result_title = mysql_query("select * from lookup_personnel_tbl where company_id='".$_SESSION['company_id']."' AND category='job titles' order by value asc");

        while($row_title = mysql_fetch_array($result_title))
        {
            $this->listJob->AddItem($row_title['value'], null, $row_title['value']);
        }

       //Vendor Region,Prov, Municipality

       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }

        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }


       function VendorRegionChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function VendorProvinceChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbProvince->getItemIndex();
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }




       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->edContactLname->Text, 'lbll');
       $this->validateRequired($this->edContactFname->Text, 'lblf');

       $this->validateRequired($this->edAddress->Text, 'lblAddress');
       $this->validateRequired($this->edCity->Text, 'lblCity');

       //$this->validateRequired($this->edEmail->Text, 'lble');
       //$this->validateRequired($this->edContactDesignation->Text, 'lbld');

       $phone = $this->edPhone->Text;
       $this->validateRequired($this->edPhone->Text, 'lblp');

       if($phone!='')
       {
       if(preg_match("/^[0-9]{3}[-]?[0-9]{4}$/", $phone))
       {
       $this->lblp->Caption = '';
       }
       else
       {
       $this->lblp->Caption = '&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $acode = $this->edArea->Text;
       if($acode!='')
       {
       if(preg_match("/^[0-9]{2,4}$/", $acode))
       {
       $this->lblarea->Caption = '';
       }
       else
       {
       $this->lblarea->Caption = 'Incorrect format - e.g.02&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $this->validateRequired($this->edArea->Text, 'lblarea');

       $email = $this->edEmail->Text;
       //if($email=='')
       //$this->validateRequired($this->edEmail->Text, 'lble');

       if($email!='')
       {
       if(preg_match("/[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$/", $email))
       {
       $this->lble->Caption = '';
       }
       else
       {
       $this->lble->Caption = '&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {
       /*
       region,
       province,
       municipality,

       '".$this->cmbRegion->getItemIndex()."',
       '".$this->cmbProvince->getItemIndex()."',
       '".$this->cmbMunicipality->getItemIndex()."',
       */
       mysql_query("INSERT INTO vendor_contact(company_id,
       vendor_num,
       fname,
       mname,
       lname,
       designation,
       bdate,
       acode,
       phone,
       mobile,
       address,
       bldg,
       city,
       country,
       fax_acode,
       fax,
       email,
       edit_who)

       VALUES
       ('".$_SESSION['company_id']."',
       '".$this->hiddenVend->getValue()."',
       '".$this->edContactFname->Text."',
       '".$this->edContactMname->Text."',
       '".$this->edContactLname->Text."',
       '".$this->cmbJob->getItemIndex()."',
       '".$this->JTBdate->Text."',
       '".$acode."',
       '".$this->edPhone->Text."',
       '".$this->edMobile->Text."',
       '".$this->edAddress->Text."',
       '".$this->edBldg->Text."',
       '".$this->edCity->Text."',
       '".$this->cmbCountry->getItemIndex()."',
       '".$this->edAreaFax->Text."',
       '".$this->edFax->Text."',
       '".$this->edEmail->Text."',
       '".$_SESSION['username']."')") or die("INSERT CONTACT - ".mysql_error());

       echo "<script language=javascript>
       alert('Added contact successfully');
       window.opener.JTContactList.location.reload();
       var answer = confirm('Add another record?');
       if(answer)
       {
       window.location.href='vendor_contact_create.php';
       }
       else
       {
       window.close();
       }
       //window.opener.document.getElementById('JTContactList').src='vendor_contact_list.php';
       </script>";
       }
       }

       function btnContactCancelClick($sender, $params)
       {
       echo '<script language=javascript>
       window.close()</script>';


       }

}

global $application;

global $Add_contact2;

//Creates the form
$Add_contact2=new Add_contact2($application);

//Read from resource file
$Add_contact2->loadResource(__FILE__);

//Shows the form
$Add_contact2->show();

?>